package com.meta.nd.filter.vo;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import com.meta.nd.util.json.NdJsonTool;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class FilterFdDeserializer extends JsonDeserializer<FilterFd> {

    @Override
    public FilterFd deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String fdName= node.get("fdName").asText();
        String dataType = node.get("dataType").asText();
        Object value = NdJsonTool.jsonNode2Fv(dataType,node.get("value"));
        String fixedValue = node.get("fixedValue")!=null && !node.get("fixedValue").isNull()? node.get("fixedValue").asText():null;
        String ndName = node.get("ndName")!=null && !node.get("ndName").isNull()?node.get("ndName").asText():null;
        int serNo = node.get("serNo") != null && !node.get("serNo").isNull()?node.get("serNo").asInt():0;
        String logicalOperator = node.get("logicalOperator") != null&& !node.get("logicalOperator").isNull()?node.get("logicalOperator").asText():null;
        String alias = node.get("fdAlias")!=null && !node.get("fdAlias").isNull()? node.get("fdAlias").asText():null;
        FilterFd filterFd = new FilterFd();
        filterFd.setFdName(fdName);
        filterFd.setFdAlias(alias);
        filterFd.setDataType(dataType);
        filterFd.setLogicalOperator(logicalOperator);
        filterFd.setValue(value);
        filterFd.setFixedValue(fixedValue);
        filterFd.setNdName(ndName);
        filterFd.setSerNo(serNo);
        return filterFd;
    }
}
